/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rps.controller;

import bean.entity.Applicants;
import bean.session.ApplicantSessionBean;
import java.io.Serializable;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.component.UIInput;
import javax.faces.event.ValueChangeEvent;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;

/**
 *
 * @author server
 */
@ManagedBean
@SessionScoped
public class ApplicantManagedBean implements Serializable {

    @EJB
    private ApplicantSessionBean applicantSessionBean;
    private Applicants app;
    private int select;
    private String name;
    private List<Applicants> listApplicant;

    /** Creates a new instance of ApplicantManagedBean */
    public ApplicantManagedBean() {
        listApplicant = new LinkedList<Applicants>();
        app = new Applicants();

    }

    public String addNewApplicant() {
        Date d = new Date();
        app.setApplicantID("");
        app.setDoc(d);
        applicantSessionBean.addNewApplicant(app);
        listApplicant = applicantSessionBean.findAllApplicant();
        return "pretty:hrgroup";
    }

    public void renderPopup(ActionEvent e) {
        String param = (String) e.getComponent().getAttributes().get("demo");
        app = applicantSessionBean.findApplicantByID(param);
    }

    public String applicantDetails(Applicants app) {
        this.app = app;
        return "pretty:applicantdetails";
    }

    public List<Applicants> getAllApplicantse() {
        listApplicant = applicantSessionBean.findAllApplicant();
        return listApplicant;
    }

    public void findIDApplicant(ValueChangeEvent e) {
        UIInput nameInput = (UIInput) e.getComponent();
        String name = (String) nameInput.getValue();
        if (name != null && select == 1) {
            listApplicant = applicantSessionBean.findIdapplicant(name);
        } else {
            listApplicant = applicantSessionBean.findApplicantName(name);
        }

    }

    public void setQuery(ValueChangeEvent e) {
        UIInput nameInput = (UIInput) e.getComponent();
        int ID = Integer.parseInt(nameInput.getValue().toString());
        select = ID;
    }

    public void updateApplicant() {
        applicantSessionBean.updateApplicant(listApplicant);
    }

    public List<Applicants> getfindAllApplicant() {
        return applicantSessionBean.findAllApplicant();
    }

    public Applicants getApplicant() {
        return app;
    }

    public void setApplicant(Applicants ap) {
        this.app = ap;
    }

    /**
     * @return the app
     */
    public Applicants getApp() {
        return app;
    }

    /**
     * @param app the app to set
     */
    public void setApp(Applicants app) {
        this.app = app;
    }

    /**
     * @return the select
     */
    public int getSelect() {
        return select;
    }

    /**
     * @param select the select to set
     */
    public void setSelect(int select) {
        this.select = select;
    }

    /**
     * @return the listApplicant
     */
    public List<Applicants> getListApplicant() {
        return listApplicant;
    }

    /**
     * @param listApplicant the listApplicant to set
     */
    public void setListApplicant(List<Applicants> listApplicant) {
        this.listApplicant = listApplicant;
    }

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
}
